/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.microedition.midlet.*;
import javax.microedition.media.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.media.control.StopTimeControl;

/**
 * @author Administrador
 */
public class Mp3WavTest extends MIDlet implements CommandListener {
    private Display display;
    private Command exitCmd, playCmd, stopCmd, preStopCmd;
    private List list;
    private InputStream is;
    private Player player;
    private Control control;

    public void startApp() {
        display = Display.getDisplay(this);

        exitCmd=new Command("Salir", Command.EXIT, 2);
        playCmd=new Command("Play", Command.SCREEN, 0);
        stopCmd=new Command("Stop", Command.SCREEN, 1);
        preStopCmd=new Command("PreStop", Command.ITEM, 1);

        list=new List("¿Mp3 o Wav?", List.EXCLUSIVE);
        list.append("Mp3", null);
        list.append("Wav", null);
        list.append("Mp3 Desde la mitad.", null);
        list.addCommand(playCmd);
        list.addCommand(exitCmd);
        list.setCommandListener(this);

        display.setCurrent(list);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if (c==exitCmd){
            destroyApp(false);
            notifyDestroyed();
        }
        else{
            if (c==playCmd){
                this.play();
            }
            else{
                if (c==stopCmd){
                    this.stop();
                    list.removeCommand(stopCmd);
                    list.addCommand(playCmd);
                }
                else{ //preStopCmd
                    try{
                        this.play();
                        this.preStop();
                        while(player.getState()==player.STARTED){
                            Alert aux=new Alert("Activo", "Dentro del while", null, null);
                            display.setCurrent(aux);
                        }

                        Alert aux2=new Alert("Finalizado", "Ciclo terminado", null, null);
                        display.setCurrent(aux2);

                        this.stop();
                        list.removeCommand(stopCmd);
                        list.removeCommand(preStopCmd);
                        list.addCommand(playCmd);
                    }catch(Exception e){manageException(e);}
                }
            }

        }
    }

    public void playMp3(){
        initPlayer("mp3");
        try{
            player.start();
        }catch(Exception e){this.manageException(e);}
    }
    
    public void playMp3(long num, long den){
        if (num==0 || den==0)
            playMp3();
        initPlayer("mp3");
        try{
            player.prefetch();
            player.realize();
            long now=player.getDuration()*num/den;
            player.setMediaTime(now);
            player.start();
        }catch(Exception e){this.manageException(e);}
    }

    public void playWav(){
        initPlayer("wav");
        try{
            player.start();
        }catch(Exception e){this.manageException(e);}
    }

    private void initPlayer(String file) {
        String aux=null;
        try{
            if(file.equals("mp3")){
                file="/prueba.mp3";
                aux="audio/mpeg";
            }else{
                file="/prueba.wav";
                aux="audio/x-wav";
            }
            is=getClass().getResourceAsStream(file);
            player=Manager.createPlayer(is, aux);
        }catch(Exception e){manageException(e);}
    }

    private void manageException(Exception e){
        Alert aux=new Alert("Exception", e.toString(), null, null);
        aux.setTimeout(Alert.FOREVER);
        display.setCurrent(aux);
        e.printStackTrace();
    }

    private void stop(){
        try{
            player.stop();
            player.deallocate();
        }catch(Exception e){manageException(e);}
    }
    
    private void play(){
        switch (list.getSelectedIndex()){
                case 0:
                    playMp3();
                    break;
                case 1:
                    playWav();
                    break;
                case 2:
                    playMp3(1,2);
                }
                list.removeCommand(playCmd);
                list.addCommand(stopCmd);
                list.addCommand(preStopCmd);
    }

    private void preStop(){
        StopTimeControl s=(StopTimeControl) player.getControl("StopTimeControl");
        s.setStopTime(player.getDuration()/2);
    }
}
